Boonrit kidngan
Interface ใน View class ประกอบด วย View.OnClickListener เม อม การ click View.OnLongClickListener เม อม การ click ค าง View.OnFocusChangeListener เม อเปล ยนการเล อก View.OnKeyListener เม อม การกด key View.OnTouchListener ม การส มผ สหน าจอ
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tlayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="button1" android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content"></button> <Button android:text="button2" android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content"></button> <Button android:text="button3" android:id="@+id/button3" android:layout_width="fill_parent"android:layout_height="wrap_content"></button> <EditText android:layout_width="fill_parent" android:id="@+id/edittext1" android:layout_height="wrap_content"> <requestfocus></requestfocus> </EditText> </LinearLayout>
public class TestClick extends Activity implements OnClickListener{ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button b=(button) findviewbyid(r.id.button1); b.setonclicklistener(this); } public void onclick(view v) { // TODO Auto-generated method stub Toast.makeText(this, "onclick", Toast.LENGTH_SHORT).show(); } }
public class TestLab1_4Activity extends Activity implements OnLongClickListener{ /** Called when the activity is first created. */ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button b=(button) findviewbyid(r.id.button1); b.setonlongclicklistener(this); } public boolean onlongclick(view arg0) { // TODO Auto-generated method stub Toast.makeText(this, "onlongclick", Toast.LENGTH_SHORT).show(); return false; } }
public class TestFocusChange extends Activity implements OnFocusChangeListener{ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button b1=(button) findviewbyid(r.id.button1); Button b2=(button) findviewbyid(r.id.button2);button b3=(button) findviewbyid(r.id.button3); b1.setonfocuschangelistener(this); b2.setonfocuschangelistener(this); b3.setonfocuschangelistener(this); } public void onfocuschange(view v, boolean hasfocus) { { } { } { } } // TODO Auto-generated method stub if(v.getid()==r.id.button1) Toast.makeText(this, "button1", Toast.LENGTH_SHORT).show(); else if(v.getid()==r.id.button2) Toast.makeText(this, "button2", Toast.LENGTH_SHORT).show(); else if(v.getid()==r.id.button3) Toast.makeText(this, "button3", Toast.LENGTH_SHORT).show(); }
public class TestKeyPress extends Activity{ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } { { } } } public boolean onkeydown(int keycode,keyevent event) switch(keycode) case KeyEvent.KEYCODE_BACK: Toast.makeText(this, "Back", Toast.LENGTH_SHORT).show(); return true; case KeyEvent.KEYCODE_SEARCH: Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show(); return true; case KeyEvent.KEYCODE_VOLUME_UP: Toast.makeText(this, "VOLUME_UP", Toast.LENGTH_SHORT).show(); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: Toast.makeText(this, "VOLUME_DOWN", Toast.LENGTH_SHORT).show(); return true; return false;
public class TestOnKey extends Activity implements OnKeyListener{ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); EditText et=(edittext) findviewbyid(r.id.edittext1); et.setonkeylistener(this); } public boolean onkey(view v, int keycode, KeyEvent event) { // TODO Auto-generated method stub if (keycode == KeyEvent.KEYCODE_ENTER) { Toast.makeText(this, "key"+keycode, Toast.LENGTH_SHORT).show(); return true; } } return false; }
public class TestOnTouch extends Activity implements OnTouchListener{ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); View t= findviewbyid(r.id.tlayout); t.setontouchlistener(this); //et.setonkeylistener(this); } public boolean ontouch(view v, MotionEvent event) { // TODO Auto-generated method stub Toast.makeText(this, ""+event.getx()+":"+event.gety(), Toast.LENGTH_SHORT).show(); return false; } }
ondown() เก ดข นเม อม การส มผ สจอภาพ onfling() เก ดข นเม อม การส มผ สจอภาพแล วลาก onlongpress() เก ดข นเม อม การส มผ สจอภาพค างไว onscroll() เก ดข นเม อม การลากน วเพ อลากออปเจ ค onshowpress() เก ดข นเม อม การส มผ สจอภาพ ย งไม ม การลากหร อ ปล อยน ว onsingletapup() เก ดข นเม อม การปล อยน ว
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="textview" android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview> <TextView android:text="textview" android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview> </LinearLayout>
public class TestOnTouch1 extends Activity implements OnTouchListener,OnGestureListener{ private GestureDetector gesturescanner; private static final int LARGE_MOVE = 60; TextView tv,tv2; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); gesturescanner=new GestureDetector(this); setcontentview(r.layout.main1); View t= findviewbyid(r.id.layout1); tv=(textview) findviewbyid(r.id.textview1); tv2=(textview) findviewbyid(r.id.textview2); t.setontouchlistener(this); //t.seton //et.setonkeylistener(this); }
public boolean ontouch(view v, MotionEvent event) { return gesturescanner.ontouchevent(event); } public boolean ondown(motionevent e) { tv.settext("-" + "DOWN" + "-"); } return true;
public boolean onfling(motionevent e1, MotionEvent e2, float velocityx, float velocityy) { if(e1.gety()- e2.gety()>large_move) { } { } { } { } } tv2.settext("fling Up with velocity "+velocityy); else if(e2.gety()- e1.gety()>large_move) tv2.settext("fling Down with velocity "+velocityy); if(e1.getx()- e2.getx()>large_move) tv2.settext("fling Left with velocity "+velocityy); else if(e2.gety()- e1.gety()>large_move) tv2.settext("fling Right with velocity "+velocityy); return true;
public void onlongpress(motionevent e) { // TODO Auto-generated method stub //Toast.makeText(this, "LongPress", Toast.LENGTH_SHORT).show(); } public boolean onscroll(motionevent e1, MotionEvent e2, float distancex, float distancey) { //tv.settext("-" + "Scroll" + "-"); return true; } public void onshowpress(motionevent e) { //tv.settext("-" + "show Press" + "-"); } public boolean onsingletapup(motionevent e) { //Toast.makeText(this, "SingleTapUp", Toast.LENGTH_SHORT).show(); return true; } }